package nimy.info.android.dinnertime.page;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabWidget;

import com.astuetz.viewpager.extensions.IndicatorLineView;

/**
 * 
 * @author 234aini@gmail.com 2012-10-14
 * 
 */
public class TabIndicatorLineView extends IndicatorLineView {
	private TabHost mTabHost;

	public TabIndicatorLineView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	public TabIndicatorLineView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public TabIndicatorLineView(Context context) {
		super(context);
	}

	@Override
	public void onPageSelected(int position) {
		tabChanged(position);
		super.onPageSelected(position);
	}

	private void tabChanged(int position) {
		// Unfortunately when TabHost changes the current tab, it kindly
		// also takes care of putting focus on it when not in touch mode.
		// The jerk.
		// This hack tries to prevent this from pulling focus out of our
		// ViewPager.
		if (getmTabHost() != null) {
			TabWidget widget = mTabHost.getTabWidget();
			int oldFocusability = widget.getDescendantFocusability();
			widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
			mTabHost.setCurrentTab(position);
			widget.setDescendantFocusability(oldFocusability);
		}
	}

	public TabHost getmTabHost() {
		return mTabHost;
	}

	public void setmTabHost(TabHost mTabHost) {
		this.mTabHost = mTabHost;
	}

}
